package com.visionet.school.app.web.repository.dynamic.impl;

import com.visionet.school.app.web.common.utils.PageUtils;
import com.visionet.school.app.web.entity.dynamic.vo.DynamicVo;
import com.visionet.school.app.web.repository.dynamic.DynamicLikesRepository;
import com.visionet.school.app.web.repository.member.MemberRepository;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by hxpeng on 2017/4/5.
 */
public class DynamicRepositoryImpl {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Resource
    private MemberRepository memberRepository;

    @Resource
    private DynamicLikesRepository dynamicLikesRepository;

    /**
     * 根据班级ID查询
     * @param dynamicVo
     * @return
     */
    public Page<DynamicVo> findPage(DynamicVo dynamicVo){
        StringBuffer sql = new StringBuffer();
        List<Object> params = new ArrayList<>();
        sql.append("select td.id,td.member_id,td.member_name,td.student_name,td.member_type,td.member_avatar_path,td.member_remark,td.class_id,td.title,td.tags,td.likes," +
                "td.on_top,td.essence,td.thumbnail,td.create_date from t_dynamic as td ");
        sql.append("where td.is_deleted = '0' ");
        sql.append("and td.class_id in (");
        String[] classIds = dynamicVo.getClassId().split(";");
        for(String s : classIds){
            sql.append("'").append(s).append("',");
        }
        StringBuffer sqlStr = new StringBuffer(sql.substring(0,sql.length()-1));
        sqlStr.append(")");

        PageRequest page = PageUtils.getPageRequestInfo(dynamicVo.getPageInfo());
        if(page == null){
            page = new PageRequest(0, 10);
        }
        int currentPage = page.getPageNumber();
        int pageSize = page.getPageSize();
        int size = jdbcTemplate.queryForInt("select count(*) from ("+sqlStr.toString()+") as c", params.toArray());

        List<DynamicVo> list = jdbcTemplate.query("select t.* from ("+sqlStr+") as t limit  "+ currentPage * pageSize+","+pageSize, params.toArray(), new RowMapper<DynamicVo>() {
            @Override
            public DynamicVo mapRow(ResultSet rs, int rowNum) throws SQLException {
                DynamicVo entity = new DynamicVo();
                entity.setId(rs.getString("id"));
                entity.setMemberId(rs.getString("member_id"));
                entity.setMemberName(rs.getString("member_name"));
                entity.setStudentName(rs.getString("student_name"));
                entity.setMemberType(rs.getString("member_type"));
                entity.setMemberAvatarPath(rs.getString("member_avatar_path"));
                entity.setTitle(rs.getString("title"));
                entity.setOnTop(rs.getInt("on_top"));
                entity.setLikes(rs.getInt("likes"));
                entity.setEssence(rs.getString("essence"));
                String thumbnails = rs.getString("thumbnail");
                if(StringUtils.isNotBlank(thumbnails)){
                    String[] thumbnailArray = thumbnails.split(";");
                    if(thumbnailArray.length > 1){
                        List<String> strings = new ArrayList<String>();
                        for(String s : thumbnailArray){
                            strings.add(s);
                        }
                        entity.setThumbnails(strings);
                    }else if(thumbnailArray.length == 1){
                        entity.setThumbnail(thumbnails);
                    }
                }
                entity.setCreateDate(rs.getTimestamp("create_date"));
                return entity;
            }
        });
        Page<DynamicVo> dynamicVoPage = new PageImpl<>(list, page, size);
        return dynamicVoPage;
    }


}
